package com.robinloveshadow.goodnight;

import com.robinloveshadow.goodnight.sounds.SoundsManager;

import android.app.Activity;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.ToggleButton;

public class GoSleepMain extends Activity implements OnClickListener,OnCheckedChangeListener{
	private static final String TAG = "GoSleep";
	private static final boolean D = false|Constance.D_ALL;
	
	private static final int MENU_ID_ABOUT = Menu.FIRST;
	
	//View
	private Button mGoSleep;
	private TextView mTimeSetting;
	private ToggleButton mStartService;
	
	//
	private SharedPreferences mSetting;
	private SharedPreferences.Editor mEditor;
	
	private int mHour;
	private int mMinute;
	private boolean mServiceStatus;
	
	private Toast mToastService;
//	Recorder mRecorder;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.good_night_main);
        setupViews();
    }

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
//		mRecorder.release();
	}

	private void setupViews() {
		mGoSleep = (Button) findViewById(R.id.button_go_sleep);
		mTimeSetting = (TextView)findViewById(R.id.textview_wake_up_time);
		mStartService = (ToggleButton)findViewById(R.id.toggleButton_start_service);
		
		mSetting = getSharedPreferences(getPackageName(), Context.MODE_PRIVATE);
		mEditor = mSetting.edit();
		
		mGoSleep.setOnClickListener(this);
		mTimeSetting.setOnClickListener(this);
		mStartService.setOnCheckedChangeListener(this);
		
		mHour = mSetting.getInt(Constance.KEY_HOUR, Constance.DEFAULT_HOUR);
		mMinute = mSetting.getInt(Constance.KEY_MIN, Constance.DEFAULT_MIN);
		mServiceStatus = mSetting.getBoolean(Constance.KEY_SERVICE_STATUS, Constance.DEFAULT_SERVICE_STATUS);
		mTimeSetting.setText(getTime(mHour,mMinute));
		mStartService.setChecked(mServiceStatus);
		
//		mRecorder = new Recorder();
	}

	private String getTime(int hour,int min){
		StringBuilder sb = new StringBuilder();
		if(hour < 10){
			sb.append("0");
		}
		sb.append(hour);
		
		sb.append(":");
		
		if(min < 10){
			sb.append("0");
		}
		sb.append(min);
		return sb.toString();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, MENU_ID_ABOUT, 0, R.string.about).setIcon(
				android.R.drawable.ic_menu_info_details);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		super.onPrepareOptionsMenu(menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_ID_ABOUT:
			Intent intent = new Intent();
			intent.setClass(this, AboutActivity.class);
			startActivity(intent);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	@Override
	public void onClick(View v) {
		if(v.equals(mGoSleep)){
			startService(new Intent(this,SleepService.class));
			SoundsManager.getInstance(this).setRingerMode(AudioManager.RINGER_MODE_SILENT);
			setAirplaneModeOn(this,true);
			finish();
		}else if(v.equals(mTimeSetting)){
			OpenOutDialog();
		}
	}
	private void OpenOutDialog(){  
	    new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {					
			@Override
			public void onTimeSet(TimePicker arg0, int h, int m) {
				mHour = h;
				mMinute = m;
				mTimeSetting.setText(getTime(mHour,mMinute));
				
				mEditor.putInt(Constance.KEY_HOUR, mHour);
				mEditor.putInt(Constance.KEY_MIN, mMinute);
				mEditor.commit();
			}
		}, mHour, mMinute, true).show();  
	}  
	
	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if(buttonView.equals(mStartService)){
			mServiceStatus = isChecked;
			mEditor.putBoolean(Constance.KEY_SERVICE_STATUS, mServiceStatus);
			mEditor.commit();
			toastShow(isChecked);
			if(isChecked){
				startService(new Intent(this,SleepService.class));
			}else{
				stopService(new Intent(this,SleepService.class));
			}
		}
	}
	
	private void toastShow(boolean flag){
		if(mToastService == null){
			mToastService = new Toast(this);
		}
		if(flag){
			mToastService = Toast.makeText(this, R.string.starting_service, Toast.LENGTH_SHORT);
			mToastService.cancel();
			mToastService.show();
		}else{
			mToastService = Toast.makeText(this, R.string.closing_service, Toast.LENGTH_SHORT);
			mToastService.cancel();
			mToastService.show();
		}
	}
	
	public void setAirplaneModeOn(Context context,boolean enabling) {
		// Change the system setting
		if(D) Log.d(TAG,"setAirplaneModeOn : enabling="+enabling);
		Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,enabling ? 1 : 0);

		// Post the intent

		Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
		intent.putExtra("state", enabling);
		context.sendBroadcast(intent);
	}
    
}